﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using VIT.Web.UI;
using VIT.Library.Images;
using VIT.DataHelper.DataAccess;

namespace VIT.Wedding.Admin
{
    public partial class ColorList : VITComponent
    {
        private void SearchData()
        {
            lsvColorWedding.InsertItemPosition = InsertItemPosition.None;
            lsvColorWedding.EditIndex = -1;
            lsvColorWedding.DataBind();
        }

        private void LoadLanguage()
        {
            ddlLang.DataSource = VITData.GetQuery<Language>();
            ddlLang.DataTextField = "Name";
            ddlLang.DataValueField = "Id";
            ddlLang.DataBind();

            ddlLang.SelectedValue = Session["language"].ToString();
        }

        private void deleteList(string[] listId)
        {
            List<string> pathImages = new List<string>();
            VITData.OpenTransaction();
            foreach (string id in listId)
            {
                var item = VITData.GetObject<Product_Color>(c => c.Id == int.Parse(id));
                if (item != null)
                {
                    pathImages.Add(item.ImagePath);
                }
                VITData.DeleteObject<Product_Color>(c => c.Id == int.Parse(id));
            }
            if (VITData.CommitTransaction())
            {
                foreach (var item in pathImages)
                {
                    if (System.IO.File.Exists(URLBaseDir + "Uploads/Images/Colors/Details/" + item))
                        System.IO.File.Delete(URLBaseDir + "Uploads/Images/Colors/Details/" + item);
                    if (System.IO.File.Exists(URLBaseDir + "Uploads/Images/Colors/Thumbs/" + item))
                        System.IO.File.Delete(URLBaseDir + "Uploads/Images/Colors/Thumbs/" + item);
                }
            }
            lsvColorWedding.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            lsvColorWedding.DelegateDataSource = new VIT.Pre.WSEnumerableDelegate(delegate(int skip, int take, ref int count)
                {
                    var result = VITData.GetQuery<Product_Color>().Skip(skip);
                    if (take >= 0) result.Take(take);
                    count = result.Count();
                    return result.ToArray();
                });
            if (!IsPostBack)
            {
                Session["order"] = "Orders";
                LoadLanguage();
                SearchData();
            }
        }

        protected void lsvColorWedding_ItemEditing(object sender, ListViewEditEventArgs e)
        {
            lsvColorWedding.InsertItemPosition = InsertItemPosition.None;
            lsvColorWedding.EditIndex = e.NewEditIndex;
            lsvColorWedding.DataBind();
        }

        protected void lsvColorWedding_ItemInserting(object sender, ListViewInsertEventArgs e)
        {
            lsvColorWedding.InsertItemPosition = InsertItemPosition.None;
            lsvColorWedding.EditIndex = -1;
            var txtName = e.Item.FindControl("txtName") as TextBox;
            var txtColor = e.Item.FindControl("txtColor") as TextBox;
            var txtDescription = e.Item.FindControl("txtDescription") as TextBox;
            var fupColor = e.Item.FindControl("fupColor") as FileUpload;
            var objColor = new Product_Color();
            objColor.Name = txtName.Text.Trim();
            objColor.Description = txtDescription.Text.Trim();
            objColor.Value = long.Parse(txtColor.Text.Trim(), System.Globalization.NumberStyles.HexNumber);
            if (fupColor.HasFile)
            {
                objColor.ImagePath = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupColor.FileName);
                try
                {
                    // save
                    fupColor.SaveAs(URLBaseDir + "Uploads/Images/Colors/Details/" + objColor.ImagePath);
                    new ResizeImage().Resize(URLBaseDir + "/Uploads/Images/Colors/Details/" + objColor.ImagePath, URLBaseDir + "/Uploads/Images/Colors/Thumbs/" + objColor.ImagePath, 150);
                }
                catch (Exception ex) { throw ex; }
            }
            VITData.AddObject(objColor);
            lsvColorWedding.DataBind();
        }

        protected void lsvColorWedding_ItemUpdating(object sender, ListViewUpdateEventArgs e)
        {
            lsvColorWedding.InsertItemPosition = InsertItemPosition.None;
            lsvColorWedding.EditIndex = -1;
            var item = lsvColorWedding.Items[e.ItemIndex];
            var txtName = item.FindControl("txtName") as TextBox;
            var txtColor = item.FindControl("txtColor") as TextBox;
            var txtDescription = item.FindControl("txtDescription") as TextBox;
            var fupColor = item.FindControl("fupColor") as FileUpload;

            var objColor = VITData.GetObject<Product_Color>(c => c.Id == Convert.ToInt32(e.Keys[0]));
            if (objColor != null)
            {
                objColor.Value = long.Parse(txtColor.Text.Trim(), System.Globalization.NumberStyles.HexNumber);
                objColor.Name = txtName.Text.Trim();
                objColor.Description = txtDescription.Text.Trim();
                if (fupColor.HasFile)
                {
                    try
                    {
                        // delete
                        if (System.IO.File.Exists(URLBaseDir + "Uploads/Images/Colors/Details/" + objColor.ImagePath))
                            System.IO.File.Delete(URLBaseDir + "Uploads/Images/Colors/Details/" + objColor.ImagePath);
                        if (System.IO.File.Exists(URLBaseDir + "Uploads/Images/Colors/Thumbs/" + objColor.ImagePath))
                            System.IO.File.Delete(URLBaseDir + "Uploads/Images/Colors/Thumbs/" + objColor.ImagePath);
                        
                        // save
                        objColor.ImagePath = String.Format("{0:yyMMddhhmmss}", DateTime.Now) + System.IO.Path.GetExtension(fupColor.FileName);
                        fupColor.SaveAs(URLBaseDir + "Uploads/Images/Colors/Details/" + objColor.ImagePath);
                        new ResizeImage().Resize(URLBaseDir + "/Uploads/Images/Colors/Details/" + objColor.ImagePath, URLBaseDir + "/Uploads/Images/Colors/Thumbs/" + objColor.ImagePath, 150);
                    }
                    catch (Exception ex) { throw ex; }
                }
                VITData.UpdateObject(c => c.Id == objColor.Id, objColor);
            }
            lsvColorWedding.DataBind();
        }

        protected void lsvColorWedding_ItemCanceling(object sender, ListViewCancelEventArgs e)
        {
            lsvColorWedding.InsertItemPosition = InsertItemPosition.None;
            lsvColorWedding.EditIndex = -1;
            lsvColorWedding.DataBind();
        }

        protected void ddlLang_SelectedIndexChanged(object sender, EventArgs e)
        {
            Session["language"] = ddlLang.SelectedValue;
        }

        protected void Insert_Click(object sender, EventArgs e)
        {
            lsvColorWedding.EditIndex = -1;
            lsvColorWedding.InsertItemPosition = InsertItemPosition.FirstItem;
            lsvColorWedding.DataBind();
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            string[] check = Request["itemCheckbox"].Split(',');
            deleteList(check);
        }

        protected void sortedChange_OnClick(object sender, EventArgs e)
        {
        }
    }
}